//登录页面
var imagePath = ''//图片路径
var openids=''
Page({
  data: {
    cameraStatus: true,//摄像头状态
    display_images:'',//显示图片
    src:'',
    base:'',
    userImage:{
      imageBase:''
    },//图片base64
    textSelectiontitle:true,//选择显示文字标题的类型
    textSelection: true,//选择显示文字的类型
    answerButton:true,//答题按钮
    test_again:true,//检测失败，重新检测按钮
    to:{
      openid: '',
      token: ''
    }
  },
  onLoad: function (option) {
    var that = this
    var openid ="to.openid"
    that.setData({
      [openid]: option.openid
    })
    console.log(option);
  },
  login: function (options){
    var that=this;
    var imageBases = "userImage.imageBase"
    var token ='to.token'
    const ctx=wx.createCameraContext()
    //创建相机
    ctx.takePhoto({
      quality: 'low',
      success:(res)=>{
        //console.log(res.data)
        wx.showLoading({
          title: '上传中...',
          icon:'loading',
          duration:1000
        }),
        //显示拍照的图片
        imagePath=res.tempImagePath,
          that.setData({
          cameraStatus: false,
          display_images: imagePath,
          }),
        wx.getFileSystemManager().readFile({
          filePath:imagePath,//选择图片路径
          encoding:'base64',//编码格式
          success:function(e){
            //图片数据base64
            //console.log(e.data)
            that.setData({
              [imageBases]: e.data
            })
            wx.request({
              url: 'http://10.20.58.235:8080/v1/open/user/manager/searchFace',
              method: "POST",
              data:that.data.c,
              success: function (res) {
                //返回的数据
                //console.log(res)
                //返回对比相似度
                console.log("人脸对比度"+res.data.result.user_list["0"].score)
                //判断人脸对比度
                if (res.data.result.user_list["0"].score>=90){
                  success: (res) =>{
                      wx.showModal({
                        title: '检测成功',
                        content: '检测',
                      })
                    },
                    that.setData({
                      textSelectiontitle: false,
                      textSelection: false,
                      answerButton:false,
                      onShow() {
                        wx.showTabBar();
                      }
                    })
                  wx.switchTab({
                    url: '../starting_to_work/starting_to_work',
                  })
                } else {
                  console.log(2)
                    console.log(1)
                    wx.showModal({
                      content: '检测失败，请重新检测！',
                      showCancel:false,
                      success:function(res){
                        if (res.confirm){
                          wx.redirectTo({
                            url: 'login',
                          })
                        }
                      }
                    })
                  that.setData({
                    answerButton:true,
                    test_again:false
                  })
                }
                that.setData({
                  [token]: res.data
                })
              },

              fail:function(res){
                wx.showModal({
                  title: '提示',
                  content: '上传失败...',
                  duration:false
                })
              }
            })
          },
        })
      },
      fail:function(){
        wx.showModal({
          title: '',
          content: '拍照失败，请重新拍照！',
        })
      }
    })
  },
  error(e) {
    console.log(e.detail)
  },
  onLoad: function (options) {
    this.ctx = wx.createCameraContext()
  },
  // oklogo:function(){
  //   wx.navigateTo({
  //     url: '../problem/problem',
  //   })
  // },
})